package com.ljm.shirojwt.controller;

import com.ljm.shirojwt.entity.User;
import com.ljm.shirojwt.result.Result;
import com.ljm.shirojwt.service.serviceImpl.UserServiceImpl;
import com.ljm.shirojwt.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController

public class TestController {

    @Autowired
    UserServiceImpl userService;
    @GetMapping("/test")
    public String test(){
        return "ok";
    }

    @PostMapping("/")
    public Result getUserByusername(User user){
        User userDB = userService.getByUsername(user.getUsername());
        Result result = new Result();
        return result.setCode("200").setData(userDB);
    }

    @PostMapping("/login")
    public Result login(User user){
        User userDB=userService.getByUsername(user.getUsername());
        if(userDB.getPassword().equals( user.getPassword())){
            return new Result("200","登录成功", JwtUtils.sign(userDB.getUsername(),userDB.getPassword()));
        }
        return new Result("500","无权访问");
    }

    @GetMapping("list")
    public Result list(){
        return new Result().setData(userService.listUser());
    }
}
